package juc.juccore.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 原子类用法演示
 */
public class AtomicIntegerDemo1 implements Runnable {
    private static final AtomicInteger atomicInteger = new AtomicInteger();

    public void incrementAtomic() {
        atomicInteger.getAndIncrement();
    }

    private static volatile int basicCount = 0;

    public void incrementBasic() {
        basicCount++;
    }

    public static void main(String[] args) throws InterruptedException {
        AtomicIntegerDemo1 atomicIntegerDemo1 = new AtomicIntegerDemo1();
        Thread r1 = new Thread(atomicIntegerDemo1);
        Thread r2 = new Thread(atomicIntegerDemo1);
        r1.start();
        r2.start();
        r1.join();
        r2.join();
        System.out.println("原子类的结果:" + atomicInteger.get());
        System.out.println("基本类的结果:" + basicCount);
    }

    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            incrementAtomic();
            incrementBasic();
        }
    }
}
